package com.boesch.electroOffice.presentation.appointment;

import java.io.Serializable;
import java.util.List;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.event.ActionEvent;

import org.springframework.stereotype.Controller;

import com.boesch.electroOffice.model.enums.SessionParameter;
import com.boesch.electroOffice.model.enums.SubfunctionIdentifier;
import com.boesch.electroOffice.utils.ConfigurationUtils;
import com.boesch.electroOffice.utils.PortletUtils;
import com.boesch.electroOffice.utils.RoleUtils;
import com.liferay.portal.model.Role;

@ManagedBean
@SessionScoped
@Controller
public class AppointmentNavigationController implements Serializable {

	private static final long serialVersionUID = 4742185260177447043L;
	
	private String navigationPath = "/jsf/appointment/appointment_list.xhtml";
	
	private boolean appointmentDetailView;

	public String getNavigationPath() {
		return navigationPath;
	}
	
	public void setNavigationPath(ActionEvent event) {
		String key = (String)event.getComponent().getAttributes().get("key");
		if(key.equals(SubfunctionIdentifier.ALL_APPOINTMENTS.getValue()) 
				|| key.equals(SubfunctionIdentifier.MY_APPOINTMENTS.getValue())
				|| key.equals(SubfunctionIdentifier.MY_OPEN_APPOINTMENTS.getValue())
				|| key.equals(SubfunctionIdentifier.MY_CLOSED_APPOINTMENTS.getValue())
				|| key.equals(SubfunctionIdentifier.NOT_ASSIGNED_APPOINTMENTS.getValue())) {
			navigationPath = "/jsf/appointment/appointment_list.xhtml";
			PortletUtils.setSessionAttribute(SessionParameter.SUBFUNCTION_KEY.getValue(), key);
			appointmentDetailView = false;
		} else if(key.equals(SubfunctionIdentifier.NEW_APPOINTMENT.getValue())) {
			navigationPath = "/jsf/appointment/appointment_form.xhtml";
			PortletUtils.setSessionAttribute(SessionParameter.RESET.getValue(), "true");
			appointmentDetailView = false;
		} else if(key.equals(SubfunctionIdentifier.ENTER_WORKINGHOURS.getValue())) {
			navigationPath = "/jsf/appointment/workinghours/workinghours_view.xhtml";
			appointmentDetailView = true;
		} else if(key.equals(SubfunctionIdentifier.ENTER_MATERIAL.getValue())) {
			navigationPath = "/jsf/appointment/material/material_view.xhtml";
			appointmentDetailView = true;
		} else if(key.equals(SubfunctionIdentifier.ENTER_TOOLS.getValue())) {
			navigationPath = "/jsf/appointment/tools/tools_view.xhtml";
			appointmentDetailView = true;
		} else if(key.equals(SubfunctionIdentifier.NEW_MEASUREMENT.getValue())) {
			navigationPath = "/jsf/appointment/measurement/measurement_form.xhtml";
			appointmentDetailView = true;
		} else if(key.equals(SubfunctionIdentifier.SHOW_MEASUREMENTS_TO_APPOINTMENT.getValue())) {
			navigationPath = "/jsf/appointment/measurement/measurement_list.xhtml";
			appointmentDetailView = true;
		} else {
			navigationPath = "/jsf/appointment/appointment_list.xhtml";
			appointmentDetailView = false;
		}
	}
	
	public void setNavigationPath(String navigationPath) {
		this.navigationPath = navigationPath;
	}
	
	public void navigateToAppointmentView(ActionEvent actionEvent) {
		String appointmentId = String.valueOf(actionEvent.getComponent().getAttributes().get("appointmentId"));
		PortletUtils.setSessionAttribute(SessionParameter.APPOINTMENT_ID.getValue(), appointmentId);
		setNavigationPath("/jsf/appointment/appointment_detail.xhtml");
		appointmentDetailView = true;
	}
	
	public void navigateToAppointmentEdit(ActionEvent actionEvent) {
		String appointmentId = String.valueOf(actionEvent.getComponent().getAttributes().get("appointmentId"));
		PortletUtils.setSessionAttribute(SessionParameter.APPOINTMENT_ID.getValue(), appointmentId);
		setNavigationPath("/jsf/appointment/appointment_detail.xhtml");
		appointmentDetailView = true;
	}
	
	public boolean hasPermission(String key) {
		boolean returnValue = false;
		List<Role> roles = RoleUtils.getCurrentUsersRoles();
		for(Role role : roles) {
			if(ConfigurationUtils.getRoleSubfunctionPreference(role.getName(), key)) {
				returnValue = true;
				break;
			}
		}
		return returnValue;
	}
	
	public String[] getMainLinkKeys() {
		return AppointmentSubfunctionUtils.getMainLinkKeys();
	}
	
	public String[] getButtonLinkKeys() {
		return AppointmentSubfunctionUtils.getButtonLinkKeys();
	}
	
	public String[] getSubLinkKeys() {
		return AppointmentSubfunctionUtils.getSubLinkKeys();
	}
	
	public void setAppointmentDetailView(boolean appointmentDetailView) {
		this.appointmentDetailView = appointmentDetailView;
	}
	
	public boolean isAppointmentDetailView() {
		return appointmentDetailView;
	}
}
